//
//  RegisterViewController.swift
//  心迹_01
//
//  Created by ibokan on 2017/7/12.
//  Copyright © 2017年 ibokan. All rights reserved.
//

import UIKit

class RegisterViewController: UIViewController {
    
    // 创建背景图片
    let mainImageView = UIImageView(image:UIImage(named:"beijing_16"))
    // 图片加高斯模糊
    func blurImage(imageToBlur:UIImage) -> UIImage {
        let gaussianBlurFilter = CIFilter(name: "CIGaussianBlur")
        gaussianBlurFilter?.setValue(CIImage(cgImage: imageToBlur.cgImage!), forKey:kCIInputImageKey)
        gaussianBlurFilter?.setValue(2.0, forKey: "inputRadius")
        let inputImage = CIImage(cgImage: imageToBlur.cgImage!)
        
        let outputImage = gaussianBlurFilter?.outputImage
        let context = CIContext(options: nil)
        let cgimg = context.createCGImage(outputImage!, from: inputImage.extent)
        
        return UIImage(cgImage: cgimg!)
    }
    // 创建背景图片方法
    func MainImage() -> UIImage{
        
        mainImageView.frame = CGRect(x:0, y:0, width:view.frame.width, height:view.frame.height)
        
        // 将UIImageView转换成UIImage
        UIGraphicsBeginImageContextWithOptions(mainImageView.layer.bounds.size, mainImageView.layer.isOpaque, 0.0)
        mainImageView.layer.render(in: UIGraphicsGetCurrentContext()!)
        let outputImg = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return self.blurImage(imageToBlur: outputImg!)
    }
    
    // 创建头像控件
    let headImage = UIImageView(image: UIImage(named:"touxiang_02"))
    
    // 创建头像控件方法
    func HeadImage() {
        headImage.frame = CGRect(x: 137, y: 80, width: 100, height: 100)
        
        // 切圆角
        // logo切圆角
        let imageLayer: CALayer = headImage.layer
        imageLayer.masksToBounds = true
        imageLayer.cornerRadius = headImage.frame.size.height / 2
        
        self.view.addSubview(headImage)
    }
    
    // 创建输入框
    let username = UITextField(frame: CGRect(x:47, y:200, width:280, height:40))
    let email = UITextField(frame: CGRect(x:47, y:250, width:280, height:40))
    let password = UITextField(frame: CGRect(x:47, y:300, width:280, height:40))
    let password_02 = UITextField(frame: CGRect(x:47, y:350, width:280, height:40))
    
    // 创建输入框方法
    func RegisterText() {
        
        // 设置边框样式为圆角矩形
        username.borderStyle = UITextBorderStyle.roundedRect
        password.borderStyle = UITextBorderStyle.roundedRect
        email.borderStyle = UITextBorderStyle.roundedRect
        password_02.borderStyle = UITextBorderStyle.roundedRect
        // 待选择文字
        username.placeholder = "请输入用户名"
        password.placeholder = "请输入邮箱地址"
        email.placeholder = "请输入密码"
        password_02.placeholder = "请确认密码"
        // 字数超过后缩小字号
        username.adjustsFontSizeToFitWidth = true
        password.adjustsFontSizeToFitWidth = true
        email.adjustsFontSizeToFitWidth = true
        password_02.adjustsFontSizeToFitWidth = true
        //最小可缩小的字号
        username.minimumFontSize = 14
        password.minimumFontSize = 14
        email.minimumFontSize = 14
        password_02.minimumFontSize = 14
        // 清除按钮设置，编辑时出现清除按钮
        username.clearButtonMode = .whileEditing
        password.clearButtonMode = .whileEditing
        email.clearButtonMode = .whileEditing
        password_02.clearButtonMode = .whileEditing
        // 弹出键盘设置
        username.keyboardType = UIKeyboardType.emailAddress
        password.keyboardType = UIKeyboardType.alphabet
        email.keyboardType = UIKeyboardType.emailAddress
        password_02.keyboardType = UIKeyboardType.alphabet
        // 键盘弹出
        username.becomeFirstResponder()
        password.becomeFirstResponder()
        email.becomeFirstResponder()
        password_02.becomeFirstResponder()
        // 键盘收起
        username.resignFirstResponder()
        password.resignFirstResponder()
        email.resignFirstResponder()
        password_02.resignFirstResponder()
        // return键设置
        username.returnKeyType = UIReturnKeyType.next
        password.returnKeyType = UIReturnKeyType.next
        email.returnKeyType = UIReturnKeyType.next
        password_02.returnKeyType = UIReturnKeyType.go
        // 给输入框添加图片
        username.leftView = UIImageView(image: UIImage(named: "username_03"))
        username.leftViewMode = UITextFieldViewMode.always
        password.leftView = UIImageView(image: UIImage(named: "password_03"))
        password.leftViewMode = UITextFieldViewMode.always
        email.leftView = UIImageView(image: UIImage(named: "username_03"))
        email.leftViewMode = UITextFieldViewMode.always
        password_02.leftView = UIImageView(image: UIImage(named: "password_03"))
        password_02.leftViewMode = UITextFieldViewMode.always
        
        
        self.view.addSubview(username)
        self.view.addSubview(password)
        self.view.addSubview(email)
        self.view.addSubview(password_02)
    }
    
    // 创建注册按钮
    let registerButton:UIButton = UIButton(type: .system)
    
    // 注册按钮方法
    func Registerbutton() {
        registerButton.frame = CGRect(x: 47, y: 420, width: 280, height: 40)
        registerButton.setTitle("注册", for: .normal)
        registerButton.setTitleColor(UIColor.white, for: .normal)
        registerButton.titleLabel?.font = UIFont.systemFont(ofSize: 20)
        registerButton.backgroundColor = UIColor.init(red: 93/255, green: 172/255, blue: 250/255, alpha: 100)
        //      loginButton.layer.borderWidth = 2
        registerButton.layer.cornerRadius = 6
        
        self.view.addSubview(registerButton)
    }
    
    // 创建底部按钮
    let backLogin:UIButton = UIButton(type:.system)
    
    // 跳转到登录
    func InLogin(){
        let inLogin = LoginViewController()
        self.navigationController!.pushViewController(inLogin, animated: true)
    }
    
    //创建底部按钮方法
    func BackLogin() {
        //设置按钮位置和大小
        backLogin.frame = CGRect(x:137, y:617, width:100, height:30)
        //设置按钮文字
        backLogin.setTitle("已有账号登录", for:.normal)
        // 按钮点击事件
        backLogin.addTarget(self, action:#selector(InLogin), for:.touchUpInside)
        self.view.addSubview(backLogin)
    }
    
    

override func viewDidLoad() {
    super.viewDidLoad()
    
    // 头像方法
    self.HeadImage()
    
    // 输入框方法
    self.RegisterText()
    
    // 注册按钮方法
    self.Registerbutton()
    
    // 底部按钮方法
    self.BackLogin()
    
    // 背景图片方法
    self.view.backgroundColor = UIColor(patternImage: MainImage())
    
}

//内存警告时调用
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}
}
